﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TowerDefense
{
    public class ScreenManager : DrawableGameComponent
    {

        Stack<GameScreen> gameScreens = new Stack<GameScreen>();
        public IInputHandler input;
        //spritebatch for each screen
        public SpriteBatch batch;
        //music for each screen
        //sfx for each screen

        //public AudioBank audio;

        public ScreenManager(Microsoft.Xna.Framework.Game game, SpriteBatch spriteBatch)
            : base(game)
        {
            this.input = (IInputHandler)this.Game.Services.GetService(typeof(IInputHandler));
            this.batch = spriteBatch;
            // this.audio = new AudioBank();
            // this.audio.LoadContent(this.Game.Content);
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            gameScreens.Peek().Update(gameTime);


            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            //draw the gamescreens that aren't meant to be hidden
            foreach (GameScreen gameScreen in this.gameScreens)
            {
                if (!gameScreen.isHidden)
                {
                    gameScreen.Draw(gameTime);
                }
            }

            base.Draw(gameTime);
        }

        public void addScreen(GameScreen gameScreen)
        {
            if (gameScreens.Count != 0)
            {
                gameScreens.Peek().isDisabled = true;
                gameScreens.Peek().isHidden = true; //TODO: need to address this
            }
            gameScreen.screenManager = this;
            gameScreen.LoadContent();
            this.gameScreens.Push(gameScreen);
        }

        public void addScreen(GameScreen gameScreen, bool hidden)
        {
            if (hidden == false)
                gameScreens.Peek().isHidden = false;
            else
                gameScreens.Peek().isHidden = true;

            if (gameScreens.Count != 0)
            {
                gameScreens.Peek().isDisabled = true;
            }
            gameScreen.screenManager = this;
            gameScreen.LoadContent();
            this.gameScreens.Push(gameScreen);
        }

        public void removeScreen(GameScreen gameScreen)
        {
            this.gameScreens.Pop();
            if (gameScreens.Count != 0)
            {
                gameScreens.Peek().isDisabled = false;
                gameScreens.Peek().isHidden = false;
            }
        }
    }
}

